package com.example.demo.module.api;

import com.example.demo.common.exception.RequestBodyParamException;
import com.example.demo.module.dto.UserDTO;
import com.example.demo.module.entity.User;
import com.example.demo.module.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/test")
@Tag(name = "User API", description = "用户管理相关接口")
public class TestController {

    @Resource
    private IUserService userService;

    @RequestMapping("/log")
    public List testLog(){
        log.info("---info--");
        log.warn("---warn--");
        log.error("---erro--");
        log.debug("---debug--");
        List list = new ArrayList();
        list.add("11");
        list.add("22");
        return list;
    }

    @GetMapping("/testConvert")
    @Operation(method = "testConvert", summary = "测试类转化接口")
    public UserDTO testConvert(){
        //模拟前端传递参数
        UserDTO userDTO = new UserDTO();
        userDTO.setId(1L);
        UserDTO userByUserDTO = userService.getUserByUserDTO(userDTO);
        return userByUserDTO;
    }
    @GetMapping("")
    @Operation(method = "test", summary = "测试接口")
    public String test(){
        return "test";
    }

    @GetMapping("/error")
    @Operation(method = "test", summary = "测试自定义接口")
    public String testError(){
        throw new RequestBodyParamException("请求参数不合法");
//        return "test";
    }

    @GetMapping("/user")
    @Operation(method = "allUsers", summary = "获取所有用户")
    public List<User> allUsers(){
        log.info("获取所有用户");
        return userService.list();
    }

    @GetMapping("/user/{id}")
    @Operation(method = "getUserCache", summary = "获取用户缓存")
    public void testGetUserCache(@PathVariable String id){
        User user = userService.getUserById(id);
        System.out.println(user);
    }

}
